<?php
declare (strict_types=1);

namespace app\admin\service\finance;

use app\admin\logic\finance\Recharge as RechargeLogic;
use app\admin\logic\system\Config as SystemConfig;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\facade\Request;
use think\helper\Arr;

/**
 * 充值配置
 */
class Recharge extends BaseService
{
    /**
     * 配置列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     *
     * @return   [type]                         [description]
     */
    public function index($is_ios = 0)
    {
        $data = app(RechargeLogic::class)->getList([]);

        // 定义字段
        $keyList = [
            'price'        => ListItem::make('custom')->title('支付金额'),
            'balance'      => ListItem::make('custom')->title('到账余额'),
            'give_balance' => ListItem::make('custom')->title('赠送余额'),
            'update_time'  => ListItem::make()->title('最后更新时间'),
        ];

        if ($is_ios == 1) {
            $keyList = Arr::prepend($keyList, ListItem::make()->title('内购产品ID'), 'product_id');
        }

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        // $list->searchKey = $searchKey;
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 新建配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     *
     * @return   [type]                         [description]
     */
    public function create($is_ios = 0)
    {
        if (Request::isPost()) {
            $data = Request::post();
            $res  = app(RechargeLogic::class)->createConfig($data, $is_ios);
            return $this->parseToData($res->toArray(), 1, '添加成功');
        }

        $keyList = [
            'price'        => FormItem::make('inputNumber')->title('支付金额')->min(0)->precision(2)->required(),
            'balance'      => FormItem::make('inputNumber')->title('到账余额')->min(0)->precision(2)->required(),
            'give_balance' => FormItem::make('inputNumber')->title('赠送余额')->min(0)->precision(2)->help('额外赠送余额'),
        ];
        if ($is_ios == 1) {
            $keyList = Arr::prepend($keyList, FormItem::make()->title('内购产品ID')->required()->help('需要与苹果支付后端配置一致'), 'product_id');
        }

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey    = $keyList;
        $form->saveMethod = 'POST';

        if ($is_ios) {
            $form->savePostUrl = '/admin/finance/recharge/ios/create';
        } else {
            $form->savePostUrl = '/admin/finance/recharge/create';
        }

        return $form->send();
    }

    /**
     * 编辑配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function edit(int $id, $is_ios = 0)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $res  = app(RechargeLogic::class)->editConfig($id, $data, $is_ios);
            return $this->parseToData($res->toArray(), 1, '编辑成功');
        }

        $data = app(RechargeLogic::class)->where('id', $id)->findOrFail();

        $keyList = [
            'price'        => FormItem::make('inputNumber')->title('支付金额')->min(0)->precision(2)->required(),
            'balance'      => FormItem::make('inputNumber')->title('到账余额')->min(0)->precision(2)->required(),
            'give_balance' => FormItem::make('inputNumber')->title('赠送余额')->min(0)->precision(2)->help('额外赠送余额'),
        ];

        if ($is_ios == 1) {
            $keyList = Arr::prepend($keyList, FormItem::make()->title('内购产品ID')->required()->help('需要与苹果支付后端配置一致'), 'product_id');
        }

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey     = $keyList;
        $data->use_time    = [$data->use_stime, $data->use_etime];
        $data->derive_time = [$data->derive_stime, $data->derive_etime];
        $form->datas       = $data;
        $form->saveMethod  = 'PUT';

        if ($is_ios) {
            $form->savePostUrl = '/admin/finance/recharge/ios/' . $id . '/edit';
        } else {
            $form->savePostUrl = '/admin/finance/recharge/' . $id . '/edit';
        }

        return $form->send();
    }

    /**
     * 删除配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-14
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function delete(int $id)
    {
        app(RechargeLogic::class)->deleteConfig($id);

        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 充值配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-21
     *
     * @return   [type]                         [description]
     */
    public function config()
    {
        $fields        = [
            // 比例
            'ratio' => FormItem::make('custom')->title('充值比例')->help('充值比例用于自定义充值金额'),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $key             = 'recharge.config';
        $config          = SystemConfig::get($key, ['ratio' => '100:100'], request()->mhm_id);
        $config['__key'] = $key;
        $form->datas     = $config;

        return $form->send();

    }

    /**
     * IOS配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-21
     *
     * @return   [type]                         [description]
     */
    public function configIOS()
    {
        $fields        = [
            // 比例
            'title' => FormItem::make('input')->title('名称')->help('IOS金币名称')->maxlength(2),
            // 'symbol'    => FormItem::make('input')->title('符号')->help('IOS金币符号'),
        ];
        $form          = new PageForm();
        $form->pageKey = $fields;
        $form->withSystemConfig();
        $key             = 'recharge.config.ios';
        $config          = SystemConfig::get($key, ['title' => '', 'symbol' => ''],request()->mhm_id);
        $config['__key'] = $key;
        $form->datas     = $config;

        return $form->send();

    }
}
